-
-
Notifications
You must be signed in to change notification settings - Fork 569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: create AllUnionFields type #997
base: main
Are you sure you want to change the base?
Conversation
Is there a better name for it? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RebeccaStevens IMO, the following implementation is simpler than the one proposed in this PR:
export type AllUnionFields<Union> = [Union] extends [NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | UnknownArray]
? Union
: Simplify<
SharedUnionFields<Union> &
{
readonly [P in ReadonlyKeysOfUnion<Union>]?: ValueOfUnion<Union, P>;
} & {
[P in Exclude<KeysOfUnion<Union>, ReadonlyKeysOfUnion<Union> | keyof Union>]?: ValueOfUnion<Union, P>;
}
>;
type ValueOfUnion<Union, Key> = Union extends unknown ? Key extends keyof Union ? Union[Key] : never : never;
type ReadonlyKeysOfUnion<T> = T extends unknown ? keyof {
[P in keyof T as IsEqual<{[Q in P]: T[P]}, {readonly [Q in P]: T[P]}> extends true ? P : never]: never
} : never;
This passes all the existing test cases and also uses utilities like ValueOfUnion
and ReadonlyKeysOfUnion
, which can be exposed as standalone types.
WDYT?
And this would probably also need changes similar to this. |
@som-sm I agree, that implementation is nicer. I pushed an update with that one. |
b20ac6c
to
ec1dfb3
Compare
@RebeccaStevens I have updated this PR with the following changes:
I’d appreciate if you could review these updates once. |
fix #996
TODO: Create a deep version of the type.